package com.sparsearr.datastructures.ztr.stack;

import java.util.Scanner;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/4/16 21:05
 * @Description: 栈的简单实现与测试
 * @Version: 1.0
 */
public class LinkedListStackDemo {
    public static void main(String[] args) {
        LinkedListStack stack = new LinkedListStack(4);
        Scanner scanner = new Scanner(System.in);
        String key;
        boolean loop = true;

        while (loop) {
            System.out.println("show: 显示栈");
            System.out.println("exit: 退出程序");
            System.out.println("push: 添加数据到栈");
            System.out.println("pop: 从栈取出数据");
            System.out.println("请输入你的选择");
            key = scanner.next();

            switch (key) {
                case "show":
                    stack.list();
                    break;
                case "push":
                    System.out.println("请输入一个数");
                    int value = scanner.nextInt();
                    stack.push(value);
                    break;
                case "pop":
                    try {
                        int res = stack.pop();
                        System.out.printf("出栈的数据是：%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case "exit":
                    scanner.close();
                    loop = false;
                    break;
                default:
                    System.out.println("无效的选择，请重新输入！");
                    break;
            }
        }

        System.out.println("程序退出");
    }
}

class Node {
    // 数据域
    int value;
    // 指针域
    Node next;

    public Node(int value) {
        this.value = value;
        this.next = null;
    }
}

// 链表实现栈
class LinkedListStack {
    // 栈顶指针
    private Node top;
    // 当前栈中元素数量
    private int size;
    // 栈的最大容量
    private final int maxSize;

    public LinkedListStack(int maxSize) {
        this.top = null;
        this.size = 0;
        this.maxSize = maxSize;
    }

    // 判断栈是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    // 判断栈是否已满
    public boolean isFull() {
        return size == maxSize;
    }

    // 入栈操作
    public void push(int value) {
        if (isFull()) {
            System.out.println("栈已满，无法入栈！");
            return;
        }
        // 创建新节点
        Node newNode = new Node(value);
        // 新节点指向当前栈顶
        newNode.next = top;
        // 更新栈顶指针
        top = newNode;
        size++;
        System.out.println("入栈成功：" + value);
    }

    // 出栈操作
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空，无法出栈！");
        }
        // 获取栈顶元素值
        int value = top.value;
        // 栈顶指针下移
        top = top.next;
        size--; // 减少计数器
        return value;
    }

    // 查看栈顶元素（不移除）
    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("栈空，无法查看栈顶！");
        }
        return top.value;
    }

    // 遍历栈（从栈顶到栈底输出）
    public void list() {
        if (isEmpty()) {
            System.out.println("栈空");
            return;
        }
        Node temp = top;
        while (temp != null) {
            System.out.printf("栈元素：%d\n", temp.value);
            temp = temp.next;
        }
    }
}